package cn.aitrox.ry.service.config.controller;

import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.service.config.api.CommonConfigApi;
import cn.aitrox.ry.service.config.dto.CommonConfigDto;
import cn.aitrox.ry.service.config.dto.CommonConfigItemDto;
import cn.aitrox.ry.service.config.dto.in.CommonConfigChildAddInDto;
import cn.aitrox.ry.service.config.dto.in.CommonConfigChildEditInDto;
import cn.aitrox.ry.service.config.dto.in.CommonConfigNameExistInDto;
import cn.aitrox.ry.service.config.dto.in.CommonConfigSearchInDto;
import cn.aitrox.ry.service.config.entity.DictEntity;
import cn.aitrox.ry.service.config.service.DictService;
import cn.aitrox.ry.util.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class CommonConfigController implements CommonConfigApi {

    @Autowired
    private DictService dictService;

    @PostMapping(value = "/config/common/list")
    @Override
    public CommonConfigDto commonList(@RequestParam(value = "configList", required = false) List<String> configList) {
        return dictService.commonList(configList);
    }

    @GetMapping(value = "/config/common/maxCode")
    @Override
    public String maxCode(@RequestParam("codePrefix") String codePrefix) {
        return dictService.maxCode(codePrefix);
    }

    @PostMapping(value = "/config/common/child")
    @Override
    public RespResult childAdd(@Validated @RequestBody CommonConfigChildAddInDto input) {
        return dictService.save(input.getParentId(), input.getCode(), input.getName(), input.getValue(), input.getRemark());
    }

    @PostMapping(value = "/config/common/search")
    @Override
    public CommonPageInfo<CommonConfigItemDto> search(@Validated @RequestBody CommonConfigSearchInDto input) {
        PageInfo<DictEntity> pageInfo = dictService.search(input.getPage(), input.getPageSize(), input.getParentId(), input.getName());
        List<CommonConfigItemDto> list = pageInfo.getList().stream().map(x -> {
            CommonConfigItemDto lesion = new CommonConfigItemDto();
            BeanUtil.copy(x, lesion);
            return lesion;
        }).collect(Collectors.toList());
        return new CommonPageInfo(pageInfo.getTotal(), pageInfo.getPages(), list);
    }

    @PutMapping(value = "/config/common/child")
    @Override
    public RespResult childEdit(@Validated @RequestBody CommonConfigChildEditInDto input) {
        return dictService.edit(input.getId(), input.getName(), input.getValue(), input.getRemark());
    }

    @Override
    public Boolean nameExisted(@Validated @RequestBody CommonConfigNameExistInDto input) {
        return dictService.nameExisted(input.getId(), input.getParentId(), input.getName());
    }
}
